﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Main : MonoBehaviour
{
    public Texture2D[] ElementTextures;
    public RectTransform Tile_Template;

    const int ElementSize = 24;
    int TileSize
    {
        get { return ElementSize * 2; }
    }

    Dictionary<Vector2Int, RectTransform> PresentTiles = new Dictionary<Vector2Int, RectTransform>();

    enum TilePart
    {
        LU,
        RU,
        LD,
        RD,
        count
    };

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButton(0)|| Input.GetMouseButton(1))
        {
            Vector2Int TileIndex = new Vector2Int();
            TileIndex.x = Mathf.FloorToInt(Input.mousePosition.x / TileSize);
            TileIndex.y = Mathf.FloorToInt(Input.mousePosition.y / TileSize);

            if(Input.GetMouseButton(0))//添加
            {
                if (!PresentTiles.ContainsKey(TileIndex))
                {
                    var NewTile = Instantiate(Tile_Template.gameObject).GetComponent<RectTransform>();           
                    NewTile.SetParent(Tile_Template.parent);
                    NewTile.anchoredPosition = new Vector2(TileIndex.x * TileSize, TileIndex.y * TileSize);
                    PresentTiles.Add(TileIndex, NewTile);
                    RefreshElementTexture();
                }
            }

            if(Input.GetMouseButton(1))//删除
            {
                if (PresentTiles.ContainsKey(TileIndex))
                {
                    Destroy(PresentTiles[TileIndex].gameObject);
                    PresentTiles.Remove(TileIndex);
                    RefreshElementTexture();
                }
            }
            
        }
    }

    void RefreshElementTexture()
    {
        foreach (var Tile in PresentTiles)
        {
            for (int i = 0; i < (int)TilePart.count; i++)
            {
                TilePart part = (TilePart)i;
                Tile.Value.Find(part.ToString())
                    .GetComponent<UnityEngine.UI.RawImage>().texture
                    = ElementTextures[GetExpectElementIndex(Tile.Key,part)];
            }
        }
    }
    int GetExpectElementIndex(Vector2Int TileIndex, TilePart part)
    {
        var NeighbourFilled = new System.Func<Vector2Int,bool >((NeighbourOffset) =>
        {
            return PresentTiles.ContainsKey(TileIndex + NeighbourOffset);
        });

        if(part==TilePart.LU)
        {
            bool up = NeighbourFilled(new Vector2Int(0, 1));
            bool left = NeighbourFilled(new Vector2Int(-1, 0));
            bool up_left = NeighbourFilled(new Vector2Int(-1, 1));
            if (up && left)//上和左都有
            {
                if (up_left) //左上也有
                    return 19;
                else                                        //左上没有
                    return 5;
            }
            else if (!(up || left))//上和左都没有
                return 9;
            else    //左和上有一者有
            {
                if (up)         //上
                    return 17;
                else            //左
                    return 11;
            }
        }
        if (part == TilePart.RU)
        {
            bool up = NeighbourFilled(new Vector2Int(0, 1));
            bool right = NeighbourFilled(new Vector2Int(1, 0));
            bool up_right = NeighbourFilled(new Vector2Int(1, 1));
            if (up && right)//上和右都有
            {
                if (up_right) //右上也有
                    return 18;
                else                                        //右上没有
                    return 6;
            }
            else if (!(up || right))//上和右都没有
                return 12;
            else    //右和上有一者有
            {
                if (up)         //上
                    return 20;
                else            //右
                    return 10;
            }
        }
        if (part == TilePart.LD)
        {
            bool down = NeighbourFilled(new Vector2Int(0, -1));
            bool left = NeighbourFilled(new Vector2Int(-1, 0));
            bool down_left = NeighbourFilled(new Vector2Int(-1, -1));
            if (down && left)//下和左都有
            {
                if (down_left) //左下也有
                    return 15;
                else                                        //左下没有
                    return 7;
            }
            else if (!(down || left))//下和左都没有
                return 21;
            else    //左和下有一者有
            {
                if (down)         //下
                    return 13;
                else            //左
                    return 23;
            }
        }
        if (part == TilePart.RD)
        {
            bool down = NeighbourFilled(new Vector2Int(0, -1));
            bool right = NeighbourFilled(new Vector2Int(1, 0));
            bool down_right = NeighbourFilled(new Vector2Int(1, -1));
            if (down && right)//下和右都有
            {
                if (down_right) //右下也有
                    return 14;
                else                                        //右下没有
                    return 8;
            }
            else if (!(down || right))//下和右都没有
                return 24;
            else    //右和下有一者有
            {
                if (down)         //下
                    return 16;
                else            //右
                    return 22;
            }
        }

        return 0;
    }
}
